<?php
// 渲染源数据
function init(){
  // 获取编辑行的参数id
  $id = $_GET['id'];
  // 1.连接数据库
  $connect = mysqli_connect('localhost','root','123456','users');
  // var_dump($connect);
  // 判断
  if (!$connect) {
    exit('<h1>连接数据库失败</h1>');
  }
  // echo $id;
  // 2.执行数据查询操作
  $GLOBALS['query1'] = mysqli_query($connect,'select * from info where id = '.$id.';');

  // 判断
  if (!$GLOBALS['query1']) {
    exit('<h1>数据初始化失败</h1>');
  }
  // 关闭数据库
  mysqli_close($connect);
}
// 初始化页面
init();
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>XXX管理系统</title>
  <link rel="stylesheet" href="assets/css/bootstrap.css">
  <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
  <nav class="navbar navbar-expand navbar-dark bg-dark fixed-top">
    <a class="navbar-brand" href="#">XXX管理系统</a>
    <ul class="navbar-nav mr-auto">
      <li class="nav-item active">
        <a class="nav-link" href="index.html">用户管理</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">商品管理</a>
      </li>
    </ul>
  </nav>
  <main class="container">  
    <?php $user = mysqli_fetch_assoc($GLOBALS['query1']) ?>
    <h1 class="heading">编辑“<?php echo $user['name']; ?>”</h1>
    <form action="edit_handle.php?id=<?php echo $user['id']; ?>" method="POST" enctype="multipart/form-data">
      <div class="form-group">
        <label for="avatar">头像(默认为当前)</label>
        <input type="file" class="form-control" id="avatar" name="avatar">
      </div>
      <div class="form-group">
        <label for="name">姓名</label>
        <input type="text" class="form-control" id="name" name="name" value="<?php echo $user['name']; ?>">
      </div>
      <div class="form-group">
        <label for="gender">性别</label>
        <select class="form-control" id="gender" name="gender">
          <option value="-1">请选择性别</option>
          <option value="1"<?php echo $user['gender'] === '1' ? ' selected': ''; ?>>男</option>
          <option value="0"<?php echo $user['gender'] === '0' ? ' selected': ''; ?>>女</option>
        </select>
      </div>
      <div class="form-group">
        <label for="birthday">生日</label>
        <input type="date" class="form-control" name="birthday" id="birthday" value="<?php echo $user['birthday']; ?>">
      </div>
      <input type="submit" class="btn btn-primary" value="保存">
    </form>
  </main>
</body>
</html>
